/*
 * GeoDirection.java
 *
 * Created on 27 avril 2007, 18:59
 */

package courselog.gpsutils;

/**
 * Direction vector, in geographical units.
 * To be used for scalar produc and projections.
 *
 * @author rigal
 */
public class GeoDirection {
    
    /** X coordinate of direction vector
     */
    public double x;

    /** Y coordinate of direction vector
     */
    public double y;

    /** Z coordinate of direction vector
     */
    public double z;
    
    /**
     * GeoDirection vector with coordinates 0,0,0.
     */
    public GeoDirection() {
        x=0.0d;
        y=0.0d;
        z=0.0d;
    }
    
    /**
     * GeoDirection vector with coordinates XX,YY,ZZ.
     */
    public GeoDirection(int xx, int yy, int zz) {
        x=xx;
        y=yy;
        z=zz;
    }
    
    /**
     * Compute, for a local point pt, Nort and east vectors.
     */
    public static void vectorsNorthEast(GeoPoint pt, GeoDirection dNorth, 
                                        GeoDirection dEast) {
        if(pt.lat>=GeoPoint.dummyAng) {
            pt.computeLatLonAlt();
        }
        // Local referential (North, East)
        double cosLat= Math.cos(pt.lat*Math.PI/180.0d);
        double sinLat= Math.sin(pt.lat*Math.PI/180.0d);
        double cosLon= Math.cos(pt.lon*Math.PI/180.0d);
        double sinLon= Math.sin(pt.lon*Math.PI/180.0d);
        
        // North vector
        dNorth.x=-cosLon*sinLat;
        dNorth.y=-sinLon*sinLat;
        dNorth.z=cosLat;
        double len=1.0d/Math.sqrt(dNorth.x*dNorth.x+dNorth.y*dNorth.y+
                               dNorth.z*dNorth.z);
        dNorth.x *= len;
        dNorth.y *= len;
        dNorth.z *= len;
        
        // East vector
        dEast.x=-sinLon;
        dEast.y=cosLon;
        dEast.z=0.0d;
        len=1.0d/Math.sqrt(dEast.x*dEast.x+dEast.y*dEast.y);
        dEast.x *= len;
        dEast.y *= len;
    }

    // OVERRIDDEN METHODS

    /**
     * Display (x,y,z) as a string.
     */
    public String toString() {
        return "("+x+","+y+","+z+")";
    }
}
